package com.hbean.system.interceptor;

import com.hbean.utils.HUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Description: 自定义拦截器
 * @Project: hbean
 * @Author: hjibo
 * @CreateDate: 2018-10-01 00:29
 * @ModificationHistory:
 */

@Slf4j
@Component
public class SysInterceptor implements HandlerInterceptor {

    /**
     * 执行请求前执行的操作
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /**
         * 判断session中用户信息是否为空，如果为空则跳转到登录页
         */
        HttpSession session = request.getSession();
        if (HUtil.isNotEmpty(session.getAttribute("userId"))) {
            return true;
        } else {
            log.info("拦截器：会话已失效，请重新登录");
            response.sendRedirect(request.getContextPath());
            return false;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        log.info("--------------处理请求完成后 视图渲染之前的处理操作---------------");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
//        log.info("---------------视图渲染之后的操作-------------------------0");
    }
}
